package com.googlecode.allinclusive.fw.web.taglib;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import com.googlecode.allinclusive.fw.dict.DefaultDictManager;
import com.googlecode.allinclusive.fw.dict.DictManager;
import com.googlecode.allinclusive.fw.dict.DictValue;
import com.googlecode.allinclusive.fw.springframework.utils.SpringUtils;

public class DictTag extends SimpleTagSupport {
	
	/** 数据字典类型代码 */
	private String id;
	
	/** 范围 */
	private String scope;
	
	/** 属性名 */
	private String name;
	
	/** 是否增加空数据 */
	private boolean empty;
	
	/** 空选择项的数据字典值*/
	private String emptyValue;
	
	/** 空选择项的数据字典名*/
	private String emptyLabel;
	
	/** 数据字典管理类*/
	private String className;

	/**
	 * Process the tag.
	 */
    public void doTag() throws JspException, IOException {
        if (id == null || "".equals(id)) {
            throw new JspTagException("id is required.");
        }
        if (name == null || "".equals(name)) {
            throw new JspTagException("name is required.");
        }
        
        DictManager dictMgr = null;
        if (className == null || "".equals(className)) {
        	dictMgr = SpringUtils.getBean(DefaultDictManager.class);
        } else if (className.startsWith("spring:")) {
        	dictMgr = SpringUtils.getBean(className.substring(7), DictManager.class);
        } else {
        	try {
				dictMgr = (DictManager) Class.forName(className).newInstance();
			} catch (InstantiationException e) {
				throw new JspTagException("className is error.");
			} catch (IllegalAccessException e) {
				throw new JspTagException("className is error.");
			} catch (ClassNotFoundException e) {
				throw new JspTagException("className is error.");
			}
        }

        // 指定的类型代码的数据字典信息
        List<DictValue> dictValueList = dictMgr.getDictValueList(id);
        List<DictValue> options = new ArrayList<DictValue>();
        if (empty) {
        	options.add(0, new DictValue(id, emptyValue, emptyLabel));
        }
        options.addAll(dictValueList);

        // 保存数据字典信息
        if ("page".equals(scope)) {
            getJspContext().setAttribute(name, options, PageContext.PAGE_SCOPE);        	
        } else if ("request".equals(scope)) {
            getJspContext().setAttribute(name, options, PageContext.REQUEST_SCOPE);
        } else if ("session".equals(scope)) {
            getJspContext().setAttribute(name, options, PageContext.SESSION_SCOPE);
        } else if ("application".equals(scope)) {
            getJspContext().setAttribute(name, options, PageContext.APPLICATION_SCOPE);
        } else {
            getJspContext().setAttribute(name, options, PageContext.REQUEST_SCOPE);
        }
    }

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getScope() {
		return scope;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isEmpty() {
		return empty;
	}

	public void setEmpty(boolean empty) {
		this.empty = empty;
	}

	public String getEmptyValue() {
		return emptyValue;
	}

	public void setEmptyValue(String emptyValue) {
		this.emptyValue = emptyValue;
	}

	public String getEmptyLabel() {
		return emptyLabel;
	}

	public void setEmptyLabel(String emptyLabel) {
		this.emptyLabel = emptyLabel;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}
}
